<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>选中文字变成星号</title>
    <style type="text/css">
        body {
            font-size: 24px;
        }
    </style>
    <script type="text/javascript">
        function testletter(letter) {
            if (letter >= "a" && letter <= "z") {
                return true;
            }
            if (letter >= "A" && letter <= "Z") {
                return true;
            }
            return false;
        }

        function btnok() {
            var range = window.getSelection().getRangeAt(0);
            var container = document.createElement('div');
            container.appendChild(range.cloneContents());
            txt = container.innerHTML;
            console.log("选中的文本" + txt);
            arr = txt.split("");
            txt2 = "";
            for (i = 0; i < arr.length; i++) {
                if (testletter(arr[i])) {
                    txt2 += "*";
                } else {
                    txt2 += arr[i];
                }
            }
            console.log("转换的文本" + txt2);
            txt3 = document.getElementById("divinput").innerHTML;
            txt4 = txt3.replace(txt, txt2);
            document.getElementById("divinput").innerHTML = txt4;
        }

        function btncopy() {
            txt = document.getElementById("area1").value;
            document.getElementById("divinput").innerHTML = txt;
        }
    </script>
</head>

<body>
    <div>
        <p>把要转换的文字复制进文本框，点击复制进网页，选中要转换的文字，点击转变星号</p>
        <br>
        <textarea id="area1">       
       </textarea>
    </div>
    <div id="divinput"></div>
    <div id="divoutput"></div>
    <input type="button" onclick="btnok()" value="转变星号">
    <input type="button" onclick="btncopy()" value="复制进网页">
</body>

</html>